Shader "Unlit/cesss_ceshi"
{
    Properties
    {  
        [Header(Main Properties)]
        _MainTex ("Main Texture", 2D) = "white" {}
         
        [Space]
        // [Enum(A,0,R,1,G,2,B,3,L,4)]_MainAlphaMode ("Main Use Channel=>Alpha", Float) = 0
        _MainTintColor ("Main Tint Color", Color) = (1,1,1,1)
        [Enum(None,0,Tinting,1,Multiply,2,Screen,3,Overlay,4,Normal,5)]
        _MainTintBlendMode ("Main Tint Blend Mode", Float) = 0.0

        _MainTintColor2 ("Main Tint Color 2", Color) = (1,1,1,1)
        [Enum(None,0,Tinting,1,Multiply,2,Screen,3,Overlay,4,Normal,5)]
        _MainTintBlendMode2 ("Main Tint Blend Mode", Float) = 0.0
         
        [Header(Mask Properties)]
        _MaskTex ("Mask Texture", 2D) = "white" {}
         


 
    }

    CGINCLUDE   
        #define DT_USE_OPTIMIZE
        #include "DTEffectUtil.hlsl"

        sampler2D _MainTex, _DissTex,_DistTex,_MaskTex;
        // float4 _DissTex_ST, _MaskTex_ST;

        
        float GetCustomModeValue(int customMode, float customValue, float customSpeed, float customRange[6]) 
        {
            if(customMode == 0) return customValue;
            else if(customMode == 1) return customSpeed;// frac(customSpeed * _Time.y);
            int index = clamp(customMode,0,5);
            return customRange[index];
        }

            
             
        float4 TexFilterBlend(float4 _Layer0, int _AlphaMode, float _Brightness,
            float4 _TintColor,int _TintBlendMode,
            float _FilterInvert,float _FilterGray)
        {
            //功能 
            // float _MainBrightness; int _MainAlphaMode; float4 _MainTintColor; int _MainTintBlendMode;
            // float _MainFilterInvert;float _MainFilterGray;
            // >>dt_ToInputColor  <<dt_ToOutputColor

            //Blend
            float3 finalColor = saturate(dt_ToInputColor3(_Layer0.rgb));   //
            // float4 _TintColor = dt_ToInputColor(__TintColor);
            //颜色
            float4 ccc =  dt_ToInputColor(_TintColor);
            float3 tintColor = saturate(dt_ToInputColor(_TintColor).rgb);
            float3 tintBlendModeFunc[6] =
            { 
                finalColor,                             // None
                DT_PS_COLORING(finalColor,tintColor),   //着色
                DT_PS_MULTIPLY(finalColor,tintColor),   //相乘
                DT_PS_SCREEN(finalColor,tintColor),     //滤色 
                DT_PS_OVERLAY(finalColor,tintColor),    //叠加
                DT_PS_NORMAL(finalColor,tintColor),     //正常
            };
            int index = clamp(_TintBlendMode,0,5);
            float3 finalBlend = tintBlendModeFunc[index];
            finalColor = DT_PS_ExpotColorBlendResultColor(finalBlend,finalColor,ccc.a);
            finalColor = dt_ToOutputColor3(finalColor);


            //灰度
            finalColor = DT_PS_MONO_LIGHTEN(finalColor,_FilterGray);            
            //反相
            finalColor = DT_PS_MONO_INVERT(finalColor,_FilterInvert);
            //亮度
            finalColor = finalColor * _Brightness;

            float finalAlpha = _Layer0.a;
            finalAlpha = DT_PS_MONO_CHANLLEL_TO_ALPHA(float4(_Layer0.rgb,_Layer0.a), _AlphaMode);
            return saturate(float4(finalColor,finalAlpha));
        }
    ENDCG
    SubShader
    {
       Tags
        {
            "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" 
            "PreviewType"="Plane" "CanUseSpriteAtlas"="True"
        }
        // -------------------------------------
        // Render State Commands (渲染状态命令)
        Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off 
        Fog { Mode Off }
        AlphaToMask Off
        ColorMask RGB

        // -------------------------------------
        Pass
        {
            // Name "Dissolve"
            Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
            Cull Off //Off //Back //Front //
            ZTest LEqual // Always // LEqual //            
            // ColorMask 0

            CGPROGRAM
            #pragma vertex VFXvert
            #pragma fragment VFXfrag

            
            //Properties
            //===========================================================================
            //主图属性 Main Properties
            //=====================================================================
                // UV
                    float4 _MainTex_ST;                    
                //Color
                   int _MainTintBlendMode; float4 _MainTintColor;
                   int _MainTintBlendMode2; float4 _MainTintColor2;

            
        
             
            fixed4 VFXfrag (DTVFX_v2f i) : SV_Target
            {
            //===========================================================================
                
                //基础数据
                //===================================================================== 
                    float _DTTime = _Time.y;
                    float4 BaseUV = i.uv;
                    float3 positionWS = i.positionWS;
                    float3 normalWS = i.normalWS;
                    float CustomValue[6] = {0, 0, i.Custom1.x, i.Custom1.y, i.Custom1.z, i.Custom1.w};
                 
                 
                //遮罩
                //=====================================================================
                    float4 _maskTex = DTtex2D(_MaskTex, BaseUV);

      
                 
                
                 
                //Main颜色
                //=====================================================================
                //UV变换                
                     
                    
                    // sample the texture 
                    float4 main = DTtex2D(_MainTex,BaseUV);
                    // 颜色处理
                    // _MainTintBlendMode, _MainBrightness, _MainFilterInvert, _MainFilterGray
                    
                    float3 main1 = TexFilterBlend(main, 0, 1, _MainTintColor, _MainTintBlendMode, 0, 0);
                    
                    float4 main2 = TexFilterBlend(main, 0, 1, _MainTintColor2, 1, 0, 0);
                    //float getLuminance(float3 color) {
    // 加权系数基于人眼对RGB通道的敏感度
    // return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
// }
                    main.rgb = main1;
                    main.a = 1;
                    
                    // main.rgb = max(main.r,max(main.g,main.b));
                //最终结果
                //=====================================================================
                    float3 mainColor = main.rgb;
                    float mainAlpha = main.a;
                    //
                    float3 finalColor = mainColor;
                    float finalAlpha = main.a;
                     
                    //最终
                    // finalColor = finalColor * finalAlpha; //如果需要预乘结果，取消这行注释
                    float4 finalCol = float4(finalColor, finalAlpha);
                    return finalCol;
            //===================================================================== 
            }
            ENDCG
        }
    }
}
